package com.xhs.modular.company.service.impl;

import cn.hutool.core.util.IdUtil;
import cn.hutool.json.JSONObject;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.xhs.core.message.Result;
import com.xhs.core.message.ReturnResult;
import com.xhs.modular.company.entity.CapCompanyInfoEntity;
import com.xhs.modular.company.mapper.CapCompanyInfoMapper;
import com.xhs.modular.company.param.CompanyAddParam;
import com.xhs.modular.company.param.CompanyPageParam;
import com.xhs.modular.company.service.CapCompanyInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

import static com.mybatisflex.core.query.QueryMethods.noCondition;
import static com.xhs.modular.company.entity.table.CapCompanyInfoEntityTableDef.CAP_COMPANY_INFO_ENTITY;

/**
 * @desc: 企业信息 服务层实现。
 * @projectName: spring-boot3-demo
 * @author: xhs
 * @date: 2025-06-09 周一 15:16
 */
@Slf4j
@Service
public class CapCompanyInfoServiceImpl extends ServiceImpl<CapCompanyInfoMapper, CapCompanyInfoEntity> implements CapCompanyInfoService {

    /**
     * 获取企业信息
     *
     * @return ReturnResult<Object>
     */
    @Override
    public ReturnResult<Object> getCompanyInfo() {
        log.info("-------------获取企业信息------------------");
        List<CapCompanyInfoEntity> list = queryChain()
                .select(CAP_COMPANY_INFO_ENTITY.DEFAULT_COLUMNS)
                .from(CAP_COMPANY_INFO_ENTITY)
                .list();
        return ReturnResult.build(Result.QUERY_SUCCESS).setData(list);
    }

    /**
     * 新增企业
     *
     * @param companyAddParam 入参
     * @return ReturnResult<Object>
     */
    @Override
    public ReturnResult<Object> addCompanyInfo(CompanyAddParam companyAddParam) {
        CapCompanyInfoEntity companyInfoEntity = queryChain().select(CAP_COMPANY_INFO_ENTITY.COMPANY_NAME)
                .from(CAP_COMPANY_INFO_ENTITY)
                .where(CAP_COMPANY_INFO_ENTITY.COMPANY_NAME.eq(companyAddParam.getCompanyName()))
                .one();
        if (companyInfoEntity != null) {
            return ReturnResult.build(Result.DATA_EXISTENT);
        }
        CapCompanyInfoEntity entity = CapCompanyInfoEntity.builder()
                .companyName(companyAddParam.getCompanyName())
                .companyId("F" + IdUtil.getSnowflakeNextId())
                .build();
        boolean save = save(entity);
        if (save) {
            JSONObject object = new JSONObject();
            object.set("id", entity.getId());
            object.set("name", entity.getCompanyName());
            object.set("no", entity.getCompanyId());
            return ReturnResult.build(Result.QUERY_SUCCESS).setData(object);
        }
        return ReturnResult.build(Result.ADD_ERROR);
    }

    /**
     * 分页查询企业
     *
     * @param companyPageParam 查询条件
     * @return ReturnResult<Object>
     */
    @Override
    public ReturnResult<Object> pageList(CompanyPageParam companyPageParam) {
        Page<CapCompanyInfoEntity> page = new Page<>(companyPageParam.getPageNumber(), companyPageParam.getPageSize());
        Page<CapCompanyInfoEntity> entityPage = queryChain()
                .select(CAP_COMPANY_INFO_ENTITY.DEFAULT_COLUMNS)
                .from(CAP_COMPANY_INFO_ENTITY)
                .where(StringUtils.hasLength(companyPageParam.getCompanyName()) ? CAP_COMPANY_INFO_ENTITY.COMPANY_NAME.like(companyPageParam.getCompanyName()) : noCondition())
                .and(StringUtils.hasLength(companyPageParam.getCompanyId()) ? CAP_COMPANY_INFO_ENTITY.COMPANY_ID.like(companyPageParam.getCompanyId()) : noCondition())
                .page(page);
        return ReturnResult.build(Result.QUERY_SUCCESS).setData(entityPage);
    }
}
